package com.nc.delivery_project.client.add_department;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.nc.delivery_project.client.ClientFactory;


import com.nc.delivery_project.client.request.DepartmentRequest;
import com.nc.delivery_project.shared.proxy.CityProxy;
import com.nc.delivery_project.shared.proxy.CountryProxy;
import com.nc.delivery_project.shared.proxy.DepartmentProxy;
import com.nc.delivery_project.shared.proxy.StateProvinceProxy;

import java.util.List;

public class AddDepartmentActivity extends AbstractActivity implements AddDepartmentPresenter {
    
    private ClientFactory clientFactory;
    
    private AddDepartmentView view;
    
    private List<CityProxy> cities;
    
    private List<StateProvinceProxy> states;
    
    private List<CountryProxy> countries;
    
    int statePrId = 0;
    
    int countryId = 0;

    public AddDepartmentActivity(Place place, ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
    }

    @Override
    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        if (view == null) 
            view = new AddDepartmentView();
        
        view.setPresenter(this);
        
        panel.setWidget(view);
        
        fetchCountries();
    }

   
	@Override
    public void goTo(Place place) {
        clientFactory.getPlaceController().goTo(place);
    }

    @Override
    public ClientFactory getClientFactory() {
        return clientFactory;
    }

    @Override
    public void registration() {
    	DepartmentRequest request = clientFactory.getRequestFactory().createDepartmentRequest();

    	DepartmentProxy dept = request.create(DepartmentProxy.class);
        dept.setAddress(view.addressTxtBox.getText());
        dept.setPostalCode(view.postalCodeTxtBox.getText());
        dept.setCity(cities.get(view.cityLstBox.getSelectedIndex()));

        request.create(dept).fire();
    }
    
    private void fetchCountries() {
    	clientFactory.getRequestFactory().createCountryRequest().read().fire(new Receiver<List<CountryProxy>>() {
    	@Override
    	public void onSuccess(List<CountryProxy> response) {
    		GWT.log("countries fetched");
    		countries = response;
    		                
    		fillCountryDropBox(countries);
    		}
    	});
    }

	@Override
	public void fillCountryDropBox(final List<CountryProxy> countries) {
		
		for (CountryProxy country : countries) {
			view.countryLstBox.addItem(country.getCountryTitle(), String.valueOf(country.getCountryId()));
		}
		view.countryLstBox.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent arg0) {
			
				view.stateLstBox.clear();
				view.cityLstBox.clear();
			
				countryId = countries.get(view.countryLstBox.getSelectedIndex()).getCountryId();
				fetchStates(countryId);
				
			}
		});
			 
	}
   	

	 private void fetchStates(int countryId) {
		 clientFactory.getRequestFactory().createStateProvinceRequest().loadStatePrByCountryId(countryId).with("country").fire(new Receiver<List<StateProvinceProxy>>() {
			@Override
			public void onSuccess(List<StateProvinceProxy> response) {
				GWT.log("states fetched");
                states = response;
                
                fillStateDropBox(states);
         	}
		});
			
	}	
	 
	@Override
	public void fillStateDropBox(final List<StateProvinceProxy> states) {
		for (StateProvinceProxy state : states) {
			view.stateLstBox.addItem(state.getStateTitle(), String.valueOf(state.getStateId()));
		}

		view.stateLstBox.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent arg0) {
				view.cityLstBox.clear();
				statePrId = states.get(view.stateLstBox.getSelectedIndex()).getStateId();
				fetchCities(statePrId);

			}
		});
		
	}
	
	
	 private void fetchCities(int statePrId) {
	      
	        clientFactory.getRequestFactory().createCityRequest().loadCityByStatePrId(statePrId).with("statePr").fire(new Receiver<List<CityProxy>>() {
	            @Override
	            public void onSuccess(List<CityProxy> response) {
	                GWT.log("cities fetched");
	                cities = response;
	                                              
	                fillCityDropBox(cities);
	            }
			});
	 }
	 
	 @Override
	    public void fillCityDropBox(List<CityProxy> cities) {
	        for (CityProxy city : cities) {
	            view.cityLstBox.addItem(city.getCityTitle(), String.valueOf(city.getCityId()));
	        }
	    }

}
